package com.wtwd.campus.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonArray;
import com.wtwd.campus.common.RespCode;
import com.wtwd.campus.common.RespEntity;
import com.wtwd.campus.entity.DeviceSOSList;
import com.wtwd.campus.entity.SysUser;
import com.wtwd.campus.service.SosCallService;
import com.wtwd.campus.utils.GetCurrentSysUserInfoUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author mjy
 * @date 2021/07/20
 * @description
 */
@RestController
@RequestMapping("/sosCall")
public class SosController {

    private static final Logger logger = LoggerFactory.getLogger(SosController.class);
    @Autowired
    private SosCallService sosCallService;
    @Autowired
    private GetCurrentSysUserInfoUtils getCurrentSysUserInfoUtils;


    @PostMapping("/setSosPhone")
    public RespEntity setSosPhone(@RequestBody JSONObject jsonObject) {
        logger.info("setSosPhone param", jsonObject);
        SysUser sysUser = getCurrentSysUserInfoUtils.getCurrentSysUserInfo();
        if (sysUser == null) {
            return new RespEntity(RespCode.TOKEN_UNAUTHORIZED);
        }
        String jsonString = jsonObject.getString("sosCall");
        List<DeviceSOSList> deviceSOSLists = JSONArray.parseArray(jsonString, DeviceSOSList.class);
        return sosCallService.setSosPhone(sysUser.getUserId(), deviceSOSLists);
    }

    @GetMapping("/getSosPhone/{deviceId}")
    public RespEntity getSosPhone(@PathVariable Integer deviceId) {
        logger.info("getSosPhone param", deviceId);
        List<DeviceSOSList> deviceSOSList = sosCallService.getSosCallService(deviceId);
        return new RespEntity(RespCode.SUCCESS,deviceSOSList);
    }


}
